/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.struts;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.config.ExceptionConfig;

/**
 * There seems to be a bug in struts as using ActionForward to change a module in an exception handler never gets the new module (gets null in module parameter).
 * Anyway, this exception handler gets standard ActionForward from parent Struts ExceptionHandler, inserts "/admin" in its path if necessary, sets default module and activates redirection.
 */
public class ExceptionHandler extends org.apache.struts.action.ExceptionHandler
{
	private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog( ExceptionHandler.class.getName() );

	public ActionForward execute(java.lang.Exception ex,
															 ExceptionConfig ae,
															 ActionMapping mapping,
															 ActionForm formInstance,
															 javax.servlet.http.HttpServletRequest request,
															 javax.servlet.http.HttpServletResponse response)
												throws javax.servlet.ServletException
	{
	  if (mapping.getPath().indexOf("HomeActions")>-1)//FIXME No me gusta nada esto, pero es una solución temporal para errores de las partes de un template.
	  {
			log.error(ex.getMessage(),ex);
			return mapping.findForward("directerror");
		}
		ActionForward actionforward = super.execute(ex,ae,mapping,formInstance,request,response);
		if (request.getRequestURI().indexOf("admin")>-1)
		{
			String newpath = "";
			for (String part: actionforward.getPath().split("/"))
			if (part.length()>0)
			{
				if(newpath.length()==0)
					newpath+="/"+part;
				else
				{
					if (newpath.lastIndexOf("/")==0)
						newpath+="/admin/"+part;
					else
						newpath+="/"+part;
				}
			}
			actionforward.setPath(newpath);
		}//FIXME Aquí se mete "requestURI" que es lo mismo que el "forward" de AuthorizeAction. Quitar uno de los dos.
		request.getSession().setAttribute("requestURI",request.getRequestURI()+(request.getQueryString()==null?"":"?"+request.getQueryString()));
		actionforward.setModule("/");
		actionforward.setRedirect(true);
		return actionforward;
/*	
	
		if (ex instanceof UnidentifiedUserException)
		{
			if (request.getRequestURI().indexOf("admin")==-1)
		  {
				storeException(request,"error",new ActionMessage(ex.getClass().getName(),ex.getMessage()),new ActionForward("loginform", "/loginform.do", true, null),"request");
				return mapping.findForward("loginform");
			}
		  storeException(request,"error",new ActionMessage(ex.getClass().getName(),ex.getMessage()),new ActionForward("adminloginform", "/adminloginform.do", true, null),"request");
		  ActionForward actionforward = mapping.findForward("adminloginform");
			return actionforward;
		}		
		return super.execute(ex,ae,mapping,formInstance,request,response);*/
//	  log.error(ex.getMessage(),ex);
//		storeException(request,"error",new ActionMessage(ex.getClass().getName(),ex.getMessage()),mapping.findForward("error"),"request");
//		return mapping.findForward("error");
	}
}
